<?php
namespace app\mouth\controller;
use app\mouth\common\Api,
    app\mouth\model\CircleUserModel,
    think\facade\Request;
/**
 * 圈子用户中心
 * Class CircleUserApi
 * @package app\mouth\controller
 */
class CircleUserApi extends Api{
    public function index(){
        $index['portrait']= $this->portrait;
        $index['nickname']= $this->nickname;
        return $this->jsons('40006','获取成功',$index);
    }
    /**
     * 我的粉丝
     * @return false|string
     */
    public function fans(){
        $fans = (new CircleUserModel)->fans('circle_fans',['main_id'=>$this->quanzi_id['id']],'*');
        return $this->jsons('40006','获取成功',$fans);
    }

    /**
     * 个人 信息
     */
    public function personalinformation(){
        $user = CircleUserModel::personalinformation(Request::get('id'),$this->quanzi_id['id']);
        return $this->jsons('40006','获取成功',$user);
    }
    /**
     * 个人帖子
     * @param
     * id  帖子id
     */
    public function Myarticle(){
       $tie =  (new CircleUserModel)->Myarticle((int)input('id'),$this->quanzi_id['id']);
       return $this->jsons('40006','获取成功',$tie);
    }
        /**
     * 我的关注
     * @return false|string
     */
    public function follow(){
        $follow = (new CircleUserModel)->fans('circle_follow',['u_id'=>$this->quanzi_id['id']],'*');
        return $this->jsons('40006','获取成功',$follow);
    }
    /**
     * 我的帖子
     * @return false|string
     */
    public function invitation(){
        $follow = (new CircleUserModel)->fans('circle_invitation',['u_id'=>$this->quanzi_id['id']],'*');
        return $this->jsons('40006','获取成功',$follow);
    }

    /**
     * 我的圈子
     * @return false|string
     */
    public function circle(){
        $circle = (new CircleUserModel)->fans('circle',['u_id'=>$this->quanzi_id['id']],'*');
        return $this->jsons('40006','获取成功',$circle);
    }

    /**
     * 圈子分类
     */
    public function sortEstablishCircle(){
        $EstablishCircle = (new CircleUserModel)->fans('circle_sort',[],'*');
        return $this->jsons('40006','获取成功',$EstablishCircle);
    }

    /**
     * 我的喜欢
     */
    public function likeIt(){
        $circle = CircleUserModel::It(input('id'));
        return $this->jsons('40006','获取成功',$circle);
    }

    /**
     * 我的帖子
     */
    public function myPost(){
        $myPost = CircleUserModel::allPosts(input('id'));
        return $this->jsons('40006','获取成功',$myPost);
    }

    /**
     * 图片上传
     */
    public function pictureCircle(){
        if (empty($_FILES['cover']['name'])){
            $this->error('请上传图片');
        }
        $file = request()->file('cover');
        $cover = $this->add($file);
        return $cover;
    }

    /**
     * 圈子上传
     * @param $file
     * @return string|void
     */
    public function establishCircle(){
        $date =(new CircleUserModel)->establishCircle(Request::post(),$this->quanzi_id['id']);
        if ($date === true){
            return $this->jsons('40006','成功',[]);
        }
        return $this->jsons('40003','失败',[]);
    }

    public function add($file){
        if ($file){
            $target = $file->rule('md5')->validate(['size'=>30000,'ext'=>'jpg,png,gif'])->move( '../public/image',time());
            if ($target == false){
                $this->error('请检查图片');
            }
        }
        $translation = "/image/" .$target->getsaveName();
        return $translation;
    }
}